<template>
    <div class="full"  >
      <edit-ques @on-save-ques="updateQues" :init="init"/>
    </div>
</template>

<script>
import api from '@/api'
import EditQues from './EditQues'
export default {
  components: {
    EditQues
  },
  methods: {
    init () {
      let curRow = this.$store.state.question.curRow
      let ques = { ...curRow }
      ques.options = ques.options.split('|').map((v) => {
        return { value: v.substring(1) }
      })
      ques.level = ques.level.name
      ques.type = ques.type.name
      let answer = ques.answer
      if (ques.type === '单选') {
        ques.answer = ques.answer.charCodeAt(0) - 65
      } else if (ques.type === '多选') {
        ques.answer = []
        for (let i = 0; i < answer.length; i++) {
          console.log('caonima', answer.charCodeAt(i))
          ques.answer.push(answer.charCodeAt(i) - 65)
        }
      }
      ques.creator = ''
      return ques
    },
    updateQues (ques) {
      api.editQuestion(ques).then(api.commonResp((success, data) => {
        if (success) {
          this.$message('更新成功')
        } else {
          this.$message.error(data)
        }
      }))
    }
  }
}
</script>

<style scoped lang="less">
</style>
